﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class ColumnDataFieldConverter : TypeConverter
    {
        public ArrayList values;

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value.GetType() != typeof(string))
            {
                return base.ConvertFrom(context, culture, value);
            }
            if ((this.values != null) && !this.values.Contains(value))
            {
                this.values.Add(value);
                this.values.Sort();
            }
            return value;
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (this.values == null)
            {
                Column instance = (Column) context.Instance;
                Vdc.Grid.Grid gridContainer = ((Column) context.Instance).GridContainer;
                if (gridContainer != null)
                {
                    int count = gridContainer.DataSourceColumnNames.Count;
                    if (gridContainer.DataSourceColumnNames != null)
                    {
                        this.values = gridContainer.DataSourceColumnNames;
                    }
                    else
                    {
                        this.values = new ArrayList();
                    }
                }
            }
            return new TypeConverter.StandardValuesCollection(this.values);
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

